function sleep_and_log(duration) {
  var interval = 5;
  while (duration > interval) {
    sleep(interval * 1000);
    console.log("sleep_and_log: interval ", interval, "s");
    duration = duration - interval;
  }
  if (duration > 0) {
    sleep(duration * 1000);
    console.log("sleep_and_log: duration ", duration, "s");
  }
}
function click_text(content) {
  if (exists_text(content)) {
    var uiText = text(content).findOne();
    var bounds = uiText.bounds();
    click(bounds.centerX(), bounds.centerY());
    return uiText;
  } else {
    return false;
  }
}
function click_text_starts_with(content) {
  if (exists_text_starts_with(content)) {
    var uiText = textStartsWith(content).findOne();
    var bounds = uiText.bounds();
    click(bounds.centerX(), bounds.centerY());
    return uiText;
  } else {
    return false;
  }
}
function exists_text(content) {
  return text(content).exists();
}
function exists_text_starts_with(content) {
  return textStartsWith(content).exists();
}
function get_center_point(x1, y1, x2, y2) {
  return {
    x: x1 + (x2 - x1) / 2,
    y: y1 + (y2 - y1) / 2,
  };
}
function click_point(x1, y1, x2, y2) {
  var point = get_center_point(x1, y1, x2, y2);
  return click(point.x, point.y);
}
function swipeUp(duration, distance) {
  while (duration > 0) {
    swipe(
      device.width / 2,
      device.height / 2,
      device.width / 2,
      device.height / 2 - distance,
      500
    );
    sleep(1 * 1000);
    duration = duration - 1;
  }
}

var utils = {
  sleep_and_log,
  click_text,
  click_text_starts_with,
  exists_text,
  exists_text_starts_with,
  get_center_point,
  click_point,
  swipeUp,
};

module.exports = utils;
